package com.komodo.island.gdx;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;

public class Persistence {

	public static Preferences prefs;

	public static boolean secondTime;
	public static boolean secondTourist;
	public static boolean secondScientist;
	// public static boolean secondSuit;
	// public static boolean secondBalance;
	// public static boolean secondPhoto;
	// public static boolean secondMove;
	// public static boolean secondShopping;
	// public static boolean secondUnderwater;
	// public static boolean secondCastle;

	public static int gameCompletion;
	// 0 - 100, 100 game complete perfect
	/*
	 * 0 = new game
	 * 
	 * scientist : balance 5 - 10 suit 5 - 10 photo 5 - 10 move 5 - 10
	 * 
	 * kalo uda lengkap scientist 40% -> dapet 10%
	 * 
	 * tourist : shopping 5 - 10 castle 5 - 10 underwater 5 - 10
	 * 
	 * kalo uda lengkap tourist 30% -> dapet 10%
	 * 
	 * bonus :
	 */

	public static final int SUIT = 1;
	public static final int PHOTO = 2;
	public static final int UNDERWATER = 5;
	public static final int BALANCE = 0;
	public static final int CASTLE = 4;
	public static final int MOVE = 3;
	public static final int SHOPPING = 6;

	public static int bestScore[];
	public static boolean levelUnlocked[];
	public static int playTimes[];
	public static boolean piecesUnlocked[];

	public static boolean sound; // true if on , and false if off

	public static void load() {
		prefs = Gdx.app.getPreferences(".Settings");
		if (prefs != null) {
			init();
			secondTime = prefs.getBoolean("second");
//			secondTime = false;

//			System.out.println(secondTime);
			if (!secondTime) {
				defaultState();
				save();
			} else {
				loadedState();
			}
		}
	}

	public static void save() {
		prefs = Gdx.app.getPreferences(".Settings");
		if (prefs != null) {
			prefs.putBoolean("second", secondTime);
			prefs.putBoolean("sound", sound);
			for (int i = 0; i < 7; i++) {
				prefs.putInteger("bestscore" + i, bestScore[i]);
				prefs.putBoolean("levelunlocked" + i, levelUnlocked[i]);
				prefs.putInteger("playtimes" + i, playTimes[i]);
			}
			for (int i = 0; i < 96; i++) {
				prefs.putBoolean("pieces" + (i + 1), piecesUnlocked[i]);
			}
		}
	}

	public static void defaultState() {
		secondTime = true;
		sound = true;

		secondTourist = false;
		secondScientist = false;

		for (int i = 0; i < 7; i++) {
			bestScore[i] = 0;
			levelUnlocked[i] = false;
			playTimes[i] = 0;

		}
		for (int i = 0; i < 96; i++) {
			piecesUnlocked[i] = false;
		}
		levelUnlocked[BALANCE] = true;
		levelUnlocked[SHOPPING] = false;
		levelUnlocked[CASTLE] = true;

	}

	public static void loadedState() {
		// secondSuit= prefs.getBoolean("secondsuit");
		// secondBalance= prefs.getBoolean("secondbalance");
		// secondPhoto= prefs.getBoolean("secondphoto");
		// secondMove= prefs.getBoolean("secondmove");
		// secondShopping= prefs.getBoolean("secondshopping");
		// secondUnderwater= prefs.getBoolean("secondunderwater");
		// secondCastle= prefs.getBoolean("secondcastle");

		secondTourist = prefs.getBoolean("secondtourist");
		secondScientist = prefs.getBoolean("secondscientist");

		for (int i = 0; i < 7; i++) {
			bestScore[i] = prefs.getInteger("bestscore" + i);
			levelUnlocked[i] = prefs.getBoolean("levelunlocked" + i);
			playTimes[i] = prefs.getInteger("playtimes" + i);
		}

		for (int i = 0; i < 96; i++) {
			piecesUnlocked[i] = prefs.getBoolean("pieces" + (i + 1));
		}
		secondTime = prefs.getBoolean("second");
		sound = prefs.getBoolean("sound");
	}

	public static void init() {
		bestScore = new int[8];
		levelUnlocked = new boolean[8];
		playTimes = new int[8];
		piecesUnlocked = new boolean[96];
	}

	public static void checkHighScore(int level, int score) {
		if (prefs != null)
			if (level >= 0 && level < 7) {
				// int highscore = prefs.getInteger("bestscore" + level);
				if (score > bestScore[level]) {
					prefs.putInteger("bestscore" + level, score);
					bestScore[level] = score;
				}
			}
	}

	public static boolean unlockLevel(int level) {
		if (prefs != null)
			if (level >= 0 && level < 7) {
				// boolean levelStatus =
				// prefs.getBoolean("levelunlocked"+level);
				if (!levelUnlocked[level]) {
					levelUnlocked[level] = true;
					prefs.putBoolean("levelunlocked" + level,
							levelUnlocked[level]);
					return true;
				}

			}
		return false;
	}

	public static void saveGameEnd(int level, int score, boolean win) {
		if (win) {
			checkHighScore(level, score);
			unlockLevel(level++);
			checkGameCompletion();
		} else {

		}
		playTimes[level]++;
	}

	public static void checkGameCompletion() {

	}

	public static void toggleSound() {
		if (sound) {
			sound = false;
		} else {
			sound = true;
		}
		prefs.putBoolean("sound", sound);
	}

	public static int getBestScore(int level) {
		if ((level >= 0) && (level < 7)) {
			bestScore[level] = prefs.getInteger("bestscore" + level);
//			System.out.println("GET BEST" + level + " " + bestScore[level]);
			return bestScore[level];
		}
		return 0;
	}

	public static void newBestScore(int level, int newbestScore) {
		bestScore[level] = newbestScore;
		prefs.putInteger("bestscore" + level, newbestScore);

	}

	public static void putBooleanInto(String name, Boolean bool) {
		prefs.putBoolean(name, bool);
	}

	public static boolean unlockNewPiece(int index) {
		prefs.putBoolean("pieces" + (index + 1), true);
		piecesUnlocked[index] = true;
		return true;
	}

	public static boolean checkLevelPiece(int level, int score, boolean win) {
		Random random = new Random();
		int result = 99;
		if (level == BALANCE) {
			if (win) {
				if (!piecesUnlocked[0] && score >= 0) {
					result = 0;
				} else if (!piecesUnlocked[1] && score >= 10000) {
					result = 1;

				} else if (!piecesUnlocked[2] && score >= 20000) {

					result = 2;
				} else if (!piecesUnlocked[3] && score >= 25000) {

					result = 3;
				} else {

					if (!piecesUnlocked[4] && piecesUnlocked[30]) {
						result = 4;
					} else if (!piecesUnlocked[5]  && piecesUnlocked[30]) {
						result = 5;
					} else if (!piecesUnlocked[6]&& piecesUnlocked[30]) {
						result = 6;
					}
				}

			} else {
				if (!piecesUnlocked[7] && score == 0) {

					result = 7;
				} else if (!piecesUnlocked[8] && score >= 14000) {

					result = 8;
				} else if (!piecesUnlocked[9] && score <= 15000) {

					result = 9;
				} else if (!piecesUnlocked[10] && score <= 8000) {

					result = 10;
				} else if (!piecesUnlocked[11] && random.nextBoolean()) {

					result = 11;
				} else if (!piecesUnlocked[12] && random.nextBoolean()) {

					result = 12;
				}
			}
		} else if (level == SUIT) {
			if (win) {
				if (!piecesUnlocked[13] && score >= 200) {
					result = 13;
				} else if (!piecesUnlocked[14] && score >= 500) {
					result = 14;

				} else if (!piecesUnlocked[15] && score >= 1000) {

					result = 15;
				} else if (!piecesUnlocked[16] && score >= 1500) {

					result = 16;
				} else {

					if (!piecesUnlocked[17] && random.nextBoolean()) {
						result = 17;
					} else if (!piecesUnlocked[18] && piecesUnlocked[50]) {
						result = 18;
					} else if (!piecesUnlocked[19]  && piecesUnlocked[40]) {
						result = 19;
					} else if (!piecesUnlocked[20]  && piecesUnlocked[60]) {

						result = 20;
					} else if (!piecesUnlocked[21]  && piecesUnlocked[70]) {

						result = 21;
					} else if (!piecesUnlocked[22] && piecesUnlocked[30]) {

						result = 22;
					}
				}

			} else {
				if (!piecesUnlocked[25] && random.nextBoolean()) {
					result = 25;
				} else if (!piecesUnlocked[23] && random.nextBoolean()) {

					result = 23;
				} else if (!piecesUnlocked[24] && random.nextBoolean()) {

					result = 24;
				}
			}
		}
		// else if (level == SHOPPING) {
		//
		// }
		else if (level == PHOTO) {
			if (win) {
				if (!piecesUnlocked[26] && score >= 0) {
					result = 26;
				} else if (!piecesUnlocked[27] && score >= 15000) {
					result = 27;
				} else if (!piecesUnlocked[28] && score >= 20000) {
					result = 28;
				} else if (!piecesUnlocked[29] && score >= 37000) {
					result = 29;
				} else {

					if (!piecesUnlocked[30] && piecesUnlocked[29]) {
						result = 30;
					} else if (!piecesUnlocked[31]  && piecesUnlocked[31]) {
						result = 31;
					} else if (!piecesUnlocked[32] && piecesUnlocked[32]) {
						result = 32;
					} else if (!piecesUnlocked[33] && random.nextBoolean() && piecesUnlocked[32]) {
						result = 33;
					} else if (!piecesUnlocked[34] && piecesUnlocked[33]) {
						result = 34;
					} else if (!piecesUnlocked[35] && random.nextBoolean()) {
						result = 35;
					} else if (!piecesUnlocked[36] ) {
						result = 36;
					}
				}

			} else {
				if (!piecesUnlocked[37]) {
					result = 37;
				}else if (!piecesUnlocked[38] && score <=-5000) {
					result = 38;
				} else if (!piecesUnlocked[39] && score <=-8000) {
					result = 39;
				}  else if (!piecesUnlocked[40] && score <=-9000) {
					result = 40;
				} 

			}
		} else if (level == CASTLE) {
			if (win){
				if (!piecesUnlocked[41]&& score >= 1500){
				result = 41;	
				}else if (!piecesUnlocked[42]&&score >= 2000){
					result = 42;
				}else if (!piecesUnlocked[43]&&score >= 2000){
					result = 43;
				}else if (!piecesUnlocked[44]&&score >= 2500){
					result = 44;
				}else if (!piecesUnlocked[45]&&score >= 3000){
					result = 45;
				}else if (!piecesUnlocked[46] && random.nextBoolean()&&score >= 1000){
					result = 46;
				}else if (!piecesUnlocked[47]&& random.nextBoolean()&&score >= 1500){
					result = 47;
				}else if (!piecesUnlocked[48]&& random.nextBoolean()&&score >= 2000){
					result = 48;
				}else if (!piecesUnlocked[49]&& random.nextBoolean()&&score >= 2500){
					result = 49;
				}else if (!piecesUnlocked[50]&& piecesUnlocked[49]){
					result = 50;
				}else if (!piecesUnlocked[51]&& piecesUnlocked[50]){
					result = 51;
				}else if (!piecesUnlocked[52]&& piecesUnlocked[51]){
					result = 52;
				}else if (!piecesUnlocked[53]&& piecesUnlocked[52]){
					result = 53;
				}
			}else{
				 if (!piecesUnlocked[54]&& piecesUnlocked[51]){
					result = 54;
				}else if (!piecesUnlocked[55] && score<=1000){
					result = 55;
				}
			}
		} else if (level == UNDERWATER) {
			if (win){
				if (!piecesUnlocked[56]&& piecesUnlocked[55]){
					result = 56;
				}else if (!piecesUnlocked[57]&& piecesUnlocked[1]){
					result= 57;
				}else if (!piecesUnlocked[58]&& piecesUnlocked[2]){
					result= 58;
				}else if (!piecesUnlocked[59]&& piecesUnlocked[2]){
					result= 59;
				}else if (!piecesUnlocked[60]){
					result= 60;
				}else if (!piecesUnlocked[61] && score >=1000){
					result= 61;
				}else if (!piecesUnlocked[62] && random.nextBoolean()){
					result= 62;
				}else if (!piecesUnlocked[63] && random.nextBoolean()){
					result= 63;
				}else if (!piecesUnlocked[64] && random.nextBoolean()){
					result= 64;
				}else if (!piecesUnlocked[65] && random.nextBoolean()&&score >=1000){
					result= 65;
				}else if (!piecesUnlocked[66] && random.nextBoolean()&&score >=2000){
					result= 66;
				}else if (!piecesUnlocked[67] && random.nextBoolean()&&score >=2500){
					result= 67;
				}else if (!piecesUnlocked[68] && random.nextBoolean()){
					result= 68;
				}else if (!piecesUnlocked[69] && random.nextBoolean()){
					result= 69;
				}else if (!piecesUnlocked[70] && random.nextBoolean()){
					result= 70;
				}else if (!piecesUnlocked[71] &&score >=3000){
					result= 71;
				}else if (!piecesUnlocked[72]&&score >=1500){
					result= 72;
				}else if (!piecesUnlocked[73]&&score >=2500){
					result= 73;
				}else if (!piecesUnlocked[74]&&score >=1500){
					result= 74;
				}else if (!piecesUnlocked[75]&&score >=1500){
					result= 75;
				}else if (!piecesUnlocked[76]&&score >=2500){
					result= 76;
				}else if (!piecesUnlocked[77]&&score >=1000){
					result= 77;
				}else if (!piecesUnlocked[78]&&score >=1000){
					result= 78;
				}else if (!piecesUnlocked[79]&&score >=2500){
					result= 79;
				}else if (!piecesUnlocked[80]&&score >=1500){
					result= 80;
				}
				
			}else{
				 if (!piecesUnlocked[81]){
					result=81 ;
				}else if (!piecesUnlocked[82] ){
					result= 82;
				}else if (!piecesUnlocked[83] ){
					result= 83;
				}else if (!piecesUnlocked[84] ){
					result= 84;
				}else if (!piecesUnlocked[85]){
					result= 85;
				}
			}
		} else if (level == MOVE) {
			if (win){
				if (!piecesUnlocked[86]&& piecesUnlocked[1]){
					result = 86;
				}else if (!piecesUnlocked[87]&& piecesUnlocked[2]){
					result= 87;
				}else if (!piecesUnlocked[88]&& piecesUnlocked[83]){
					result= 88;
				}else if (!piecesUnlocked[89]&& piecesUnlocked[85]){
					result= 89;
				}else if (!piecesUnlocked[90]&& score >=15000){
					result= 90;
				}else if (!piecesUnlocked[91]&& score >=15000 ){
					result= 91;
				}else if (!piecesUnlocked[92]&& score >=15000){
					result= 92;
				}else if (!piecesUnlocked[93]&& score >=15000 ){
					result= 93;
				}else if (!piecesUnlocked[94]&& score >=15000 ){
					result= 94;
				}else if (!piecesUnlocked[95] && score >=15000){
					result= 95;
				}else if (!piecesUnlocked[96] && score >=19000){
					result= 96;
				}
				
			}
		}
		if (result != 99) {
//			System.out.println(result);
			return unlockNewPiece(result);
		} else
			return false;
	}
}
